package com.wangtao.firstapp.fragments.original_author_fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.AuthorPublishOneAdapter;
import com.wangtao.firstapp.custom.MyGridView;
import com.wangtao.firstapp.modles.OriginalAuthorPublish;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 用户作者  介绍页面（发布—>晒单，文章）
 * Created by lanouhn on 16/9/7.
 */
public class PublishFragment extends Fragment {

    private SharedPreferences sp;

    private MyGridView myGridView;
    private OriginalAuthorPublish originalAuthorPublish;
    private AuthorPublishOneAdapter authorPublishOneAdapter;

    private LinearLayout lt_fragment_publish;
    private ImageView publish;
    private TextView tv;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_publish, container, false);
        myGridView= (MyGridView) view.findViewById(R.id.mygridview_fragment_publish);

        lt_fragment_publish= (LinearLayout) view.findViewById(R.id.lt_fragment_publish);
        publish= (ImageView) view.findViewById(R.id.publish_fragment_publish);
        tv= (TextView) view.findViewById(R.id.tv_fragment_publish);

        initData();
        initEvent();
        return view;
    }


    private void initData() {
        //打开文件
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        //接收作者ID
        String author_ID = sp.getString("author_iD", "2466368");

        //用户作者  介绍页面（发布—>，文章，晒单）
     String urlPublish="http://open4.bantangapp.com/users/center/publish";
     String paramsPublish="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id="+author_ID+"&page=0&pagesize=18";

        try {
            HttpUtils.doPostAsyn(urlPublish,paramsPublish, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_Publish=new Gson();
                    originalAuthorPublish=gson_Publish.fromJson(result,OriginalAuthorPublish.class);
                    if (originalAuthorPublish.getData().getPost().size()>500){
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    publish.setVisibility(View.VISIBLE);
                    tv.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    lt_fragment_publish.setVisibility(View.VISIBLE);
                    authorPublishOneAdapter=new AuthorPublishOneAdapter(getActivity(),originalAuthorPublish);
                    myGridView.setAdapter(authorPublishOneAdapter);
                    break;
            }
        }
    };

    private void initEvent() {

    }
}
